﻿using System;
using System.Linq;
using LinFu.IoC.Configuration;
using LinFu.IoC.Factories;
using LinFu.IoC.Interfaces;

namespace CoreEx.Common.Repository.Implementation
{
    [Preprocessor]
    public class RepositoryInterceptorPreProcessor : IPreProcessor
    {        
        public void Preprocess(IServiceRequest request)
        {
            if (request.ArgumentTypes.Count() > 0 && 
                request.ArgumentTypes.First() == typeof(Type) && 
                request.ServiceName == "ServiceContractInterceptor")
                if (request.ProposedArguments[0] == typeof(IRepository))
                {
                    request.ActualFactory =
                    new FunctorFactory(r => new RepositoryInterceptor((Type)request.ProposedArguments[0],request.ProposedArguments[1]));                    
                }
        }
    }
}
